package com.shenhonghui.twins.utils;

import ohos.account.AccountAbility;
import ohos.account.DistributedInfo;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

public class DeviceUtils {
    private static final HiLogLabel DEVICE_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00200, "DeviceUtils");

    private DeviceUtils() {}

    //获取GroupID
    public static String getGroupId() {
        AccountAbility account = AccountAbility.getAccountAbility();
        DistributedInfo distributeInfo = account.queryOsAccountDistributedInfo();
        return distributeInfo.getId();
    }

    //获取所有在线可用的设备
    public static List<String> getAvailableDeviceId() {
        List<String> deviceIds = new ArrayList<>();

        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        if (deviceInfoList == null) {
            return deviceIds;
        }

        if (deviceInfoList.size() == 0) {
            HiLog.info(DEVICE_LABEL, "did not find other device");
            return deviceIds;
        }

        for (DeviceInfo deviceInfo : deviceInfoList) {
            deviceIds.add(deviceInfo.getDeviceId());
        }

        return deviceIds;
    }

    //获取所有的设备
    public static List<String> getAllDeviceId() {
        List<String> deviceIds = new ArrayList<>();

        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ALL_DEVICE);
        if (deviceInfoList == null) {
            return deviceIds;
        }
        HiLog.info(DEVICE_LABEL, "deviceInfoList size " + deviceInfoList.size());
        if (deviceInfoList.size() == 0) {
            HiLog.info(DEVICE_LABEL, "did not find other device");
            return deviceIds;
        }

        for (DeviceInfo deviceInfo : deviceInfoList) {
            deviceIds.add(deviceInfo.getDeviceId());
        }

        return deviceIds;
    }

    //获取可远程连接的设备列表
    public static List<DeviceInfo> getRemoteDevice() {
        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        return deviceInfoList;
    }

    //获取当前组网下可迁移的第一个设备id
    private String getDeviceId(){
        String deviceId = "";
        List<String> outerDevices = DeviceUtils.getAvailableDeviceId();
        HiLog.info(DEVICE_LABEL, "getDeviceId DeviceUtils.getRemoteDevice() = " + outerDevices);
        if (outerDevices == null || outerDevices.size() == 0){
            HiLog.info(DEVICE_LABEL, "no other device to continue");
        }else {
            for (String item : outerDevices) {
                HiLog.info(DEVICE_LABEL, "item deviceId = " + item);
            }
            deviceId = outerDevices.get(0);
        }
        return deviceId;
    };

}
